﻿package caurina.transitions
{

	/**
	 * Generic, auxiliary functions
	 *
	 *
	 * 
	 * 
	 */

	public class AuxFunctions
	{

		/**
		 * Gets the R (xx0000) bits from a number
		 *
		 * @paramp_numNumberColor number (ie, 0xffff00)
		 * @returnNumberThe R value
		 */
		public static function numberToR(p_num:Number):Number
		{
			// The initial & is meant to crop numbers bigger than 0xffffff
			return (p_num & 0xff0000) >> 16;
		}

		/**
		 * Gets the G (00xx00) bits from a number
		 *
		 * @paramp_numNumberColor number (ie, 0xffff00)
		 * @returnNumberThe G value
		 */
		public static function numberToG(p_num:Number):Number
		{
			return (p_num & 0xff00) >> 8;
		}

		/**
		 * Gets the B (0000xx) bits from a number
		 *
		 * @paramp_numNumberColor number (ie, 0xffff00)
		 * @returnNumberThe B value
		 */
		public static function numberToB(p_num:Number):Number
		{
			return (p_num & 0xff);
		}

		/**
		 * Returns the number of properties an object has
		 *
		 * @paramp_objectObjectTarget object with a number of properties
		 * @returnNumberNumber of total properties the object has
		 */
		public static function getObjectLength(p_object:Object):uint
		{
			var totalProperties:uint = 0;
			for (var pName:String in p_object)
			{
				totalProperties++;
			}
			return totalProperties;
		}

		/* Takes a variable number of objects as parameters and "adds" their properties, from left to right. If a latter object defines a property as null, it will be removed from the final object
		    * @paramargsObject(s)A variable number of objects
		    * @returnObjectAn object with the sum of all paremeters added as properties.
		    */
		public static function concatObjects(...args):Object
		{
			var finalObject:Object = {};
			var currentObject:Object;
			for (var i : int = 0; i < args.length; i++)
			{
				currentObject = args[i];
				for (var prop:String in currentObject)
				{
					if (currentObject[prop] == null)
					{
						// delete in case is null
						delete finalObject[prop];
					}
					else
					{
						finalObject[prop] = currentObject[prop];
					}
				}
			}
			return finalObject;
		}
	}
}